/**
 * @Author       : Hejh(3010733382@qq.com)
 * @Version      : V1.0
 * @Date         : 2024-03-17 13:35:49
 * @Description  : 系统概览接口验证器
 */

const Joi = require('joi')
const R = require('@/models/Response')
const { ERROR_SYSTEM_CODE } = require('@/constants/errorCode')
const { IS_UNSAFE_INTEGER } = require('@/constants/joiSchema')

const overViewSchema = {
  targetId: IS_UNSAFE_INTEGER,
  targetType: Joi.number().valid(1, 2)
}

/**
 * @description 用户概览信息查询的参数验证
 * @param {*} ctx
 * @param {*} next
 * @returns
 */
module.exports.listSpaceChartValidate = async (ctx, next) => {
  const schema = Joi.object({
    targetId: overViewSchema.targetId.required(),
    targetType: overViewSchema.targetType.required()
  }).validate(ctx.query)

  if (schema.error) {
    ctx.body = R.error(ERROR_SYSTEM_CODE.PARAMS_SCHEMA_ERROR)
    return
  }
  // 这里用户或者部门ID不需要验证了，错的就查询不到
  ctx.newQuery = schema.value
  await next()
}
